//
//  PKView.m
//  BKDateAndTimePickerView
//
//  Created by chk on 16/3/29.
//  Copyright © 2016年 Bhavya Kothari. All rights reserved.
//

#import "PKView.h"
#import "HistoryLotteryViewController.h"
#import "HistoryModel.h"

#define MainColor [UIColor colorWithRed:144.0/255.0 green:144.0/255.0 blue:144.0/255.0 alpha:0.5]

@interface PKView()<UIPickerViewDataSource,UIPickerViewDelegate,UIViewPassValueDelegate>

@property (strong,nonatomic) UIPickerView *datePicker;
@property (strong,nonatomic) UIView  *timeSelectView;
@property (strong,nonatomic) UIButton *timeButtonLabel;
@property (strong,nonatomic) UIButton *cancelSelectTimeButton;
@property (strong,nonatomic) UIButton *determineSelectTimeButton;
@property (strong,nonatomic) NSMutableArray *myArray1;
@property (strong,nonatomic) NSMutableArray *myArray;
@property (strong,nonatomic) NSMutableArray *myArray2;
//@property (strong,nonatomic) NSString  *timeSelectedString;
@property (strong,nonatomic) NSString  *str10;
@property (strong,nonatomic) NSString  *str11;
@property (strong,nonatomic) NSString  *str12;

@property (strong, nonatomic)  NSString *labeltime_txt;
@end

@implementation PKView

-(id)initWithFrame:(CGRect)frame{
    self=[super initWithFrame:frame];
    if(self){
        [self setupViews];
    }
    return self;
}

-(id)initWithCoder:(NSCoder *)aDecoder{
    self= [super initWithCoder:aDecoder];
    if(self){
        [self setupViews];
    }
    return self;
}


-(void)setupViews{
    NSDateFormatter *formatter4 = [[NSDateFormatter alloc] init];
    [formatter4 setDateFormat:@"dd"];
    //初始时间选择文字
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"MM"];
    self.str12 =[formatter stringFromDate:[NSDate date]];
    self.str10 = [formatter stringFromDate:[NSDate date]];
    NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init];
    [formatter1 setDateFormat:@"yyyy"];
    self.str11 = [formatter1 stringFromDate:[NSDate date]];
    self.timeSelectedString = [NSString stringWithFormat:@"%@-%@-%@",self.str11,self.str10,self.str12];
    //设置pickerview初始默认
    NSDateFormatter *formatter5 = [[NSDateFormatter alloc] init];
    [formatter5 setDateFormat:@"dd"];
    NSString *str333 = [formatter5 stringFromDate:[NSDate date]];
    
    NSDateFormatter *formatter2 = [[NSDateFormatter alloc] init];
    [formatter2 setDateFormat:@"MM"];
    NSString *str111 = [formatter2 stringFromDate:[NSDate date]];
    
    NSDateFormatter *formatter3 = [[NSDateFormatter alloc] init];
    [formatter3 setDateFormat:@"yyyy"];
    NSString *str222 = [formatter3 stringFromDate:[NSDate date]];
    int a = [str111 intValue];
    int b = [str222 intValue];
    int c = [str333 intValue];
    [self.datePicker selectRow:a-1 inComponent:1 animated:YES];
    [self.datePicker selectRow:b-1900 inComponent:0 animated:YES];
    [self.datePicker selectRow:c inComponent:2
                      animated:YES];
    [self addSubview:self.timeSelectView];
    [UIView animateWithDuration:0.3 animations:^{
        self.timeSelectView.frame = CGRectMake(0, BCWScreenH-249, BCWScreenW, 250);
    }];
}

#pragma  mark - timeSelected
-(UIView *)timeSelectView{
    if (!_timeSelectView) {
        _timeSelectView = [[UIView alloc]initWithFrame:CGRectMake(0, BCWScreenH, BCWScreenW, 250)];
        _timeSelectView.backgroundColor = [UIColor whiteColor];
        [_timeSelectView addSubview:self.datePicker];
        [_timeSelectView addSubview:self.cancelSelectTimeButton];
        [_timeSelectView addSubview:self.determineSelectTimeButton];
    }
    return _timeSelectView;
}

-(UIPickerView *)datePicker{
    if (!_datePicker) {
        _datePicker = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 0, BCWScreenW, 200)];
        _datePicker.backgroundColor = [UIColor whiteColor];
        _datePicker.dataSource = self;
        _datePicker.delegate = self;
    }
    return _datePicker;
}

-(UIButton *)cancelSelectTimeButton{
    if (!_cancelSelectTimeButton) {
        _cancelSelectTimeButton = [[UIButton alloc]initWithFrame:CGRectMake(30, 200, 70, 30)];
        _cancelSelectTimeButton.backgroundColor = MainColor;
        _cancelSelectTimeButton.layer.cornerRadius = 5;
        _cancelSelectTimeButton.layer.masksToBounds = YES;
        [_cancelSelectTimeButton setTitle:@"取消" forState:UIControlStateNormal];
        [_cancelSelectTimeButton addTarget:self action:@selector(cancelSelectTimeButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelSelectTimeButton;
}


-(UIButton *)determineSelectTimeButton{
    if (!_determineSelectTimeButton) {
        _determineSelectTimeButton = [[UIButton alloc]initWithFrame:CGRectMake(BCWScreenW-110, 200, 70, 30)];
        _determineSelectTimeButton.backgroundColor = MainColor;
        _determineSelectTimeButton.layer.cornerRadius = 5;
        _determineSelectTimeButton.layer.masksToBounds = YES;
        [_determineSelectTimeButton setTitle:@"确定" forState:UIControlStateNormal];
        [_determineSelectTimeButton addTarget:self action:@selector(determineSelectTimeButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _determineSelectTimeButton;
}
//取消
-(void)cancelSelectTimeButtonAction{
    [UIView animateWithDuration:0.3 animations:^{
        self.timeSelectView.frame = CGRectMake(0, BCWScreenH, BCWScreenW, 250);
    }];
}

- (void)passValue:(NSString *)value{
    self.labeltime_txt = value;
}

//确定
-(void)determineSelectTimeButtonAction{
    
    self.labeltime_txt = self.timeSelectedString;
    [UIView animateWithDuration:0.3 animations:^{
        self.timeSelectView.frame = CGRectMake(0, BCWScreenH, BCWScreenW, 250);
    }];
    if (self.delegate) {
        [self.delegate passValue:self.timeSelectedString];
    }
    
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 3;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    
    if (component == 0) {
        self.str11 = self.myArray[row];
    }else if (component == 1){
        self.str10 = self.myArray1[row];
    }else{
        self.str12 = self.myArray2[row];
    }
     self.timeSelectedString = [NSString stringWithFormat:@"%@-%@-%@",self.str11,self.str10,self.str12];
    
    if (self.delegate) {
        [self.delegate passValue:self.timeSelectedString];
        
    }
    
    
    
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (component == 0) {
        return 200;
    }else if (component ==1){
        return 12;
    }else
    {
        return 31;
    }
}

- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (component == 0) {
        return  self.myArray[row];
    }else if(component == 1){
        return  self.myArray1[row];
    }else
    {
        return self.myArray2[row];
    }
}


-(NSMutableArray *)myArray1{
    if (!_myArray1) {
        self.myArray1 = [[NSMutableArray alloc]init];
        for (int i = 1; i<13; i++) {
            NSString *str = [NSString stringWithFormat:@"%d",i];
            [self.myArray1 addObject:str];
        }
    }
    return _myArray1;
}

-(NSMutableArray *)myArray{
    if (!_myArray) {
        self.myArray = [[NSMutableArray alloc]init];
        for (int i = 1900; i<2100; i++) {
            NSString *str = [NSString stringWithFormat:@"%d",i];
            [self.myArray addObject:str];
        }
    }
    return _myArray;
}

- (NSMutableArray *)myArray2
{
    if (!_myArray2) {
        _myArray2 = [[NSMutableArray alloc] init];
        for (int i = 1; i<32; i++) {
            NSString *str = [NSString stringWithFormat:@"%d",i];
            [self.myArray2 addObject:str];
        }
        
    }
    return _myArray2;
}


@end
